package com.jumi.microservice.material.listener.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jumi.microservice.material.domain.JmMaterialAttributeVo;
import com.jumi.microservice.material.service.impl.JmMaterialAttributeServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * 属性表上传监听器
 *
 * @author zhangy
 */
public class AttrExcelListener extends AnalysisEventListener<JmMaterialAttributeVo> {

    //注意: 执行具体业务操作的service需要构造传参,不能IOC注入.
    private JmMaterialAttributeServiceImpl attributeService;

    private Long uid;

    private String name;

    //创建List数据结构供重写的AnalysisEventListener的接口使用
    private List<JmMaterialAttributeVo> list = new ArrayList<>();

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param materialAttributeVo
     *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(JmMaterialAttributeVo materialAttributeVo, AnalysisContext context) {
        list.add(materialAttributeVo);
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        attributeService.importAttrExcel(list,uid,name);
    }

    public AttrExcelListener(JmMaterialAttributeServiceImpl attributeService, Long uid, String name) {
        this.uid = uid;
        this.name = name;
        this.attributeService = attributeService;
    }
}
